package com.qf.fmall01.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.fmall01.dao.ProductCommentsMapper;
import com.qf.fmall01.entity.ProductCommentsVO;
import com.qf.fmall01.service.ProductCommentsService;
import com.qf.fmall01.vo.PageVO;
import com.qf.fmall01.vo.ResStatus;
import com.qf.fmall01.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductCommentsServiceImpl implements ProductCommentsService {

    @Autowired
    private ProductCommentsMapper productCommentsMapper;


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO detailCommonts(Integer productId, Integer pageNum, Integer limit) {

        ResultVO resultVO = new ResultVO();

        PageHelper.startPage(pageNum,limit);
        List<ProductCommentsVO> productCommentsVOList = productCommentsMapper.detailCommonts(productId);

        PageInfo<ProductCommentsVO> productCommentsVOPageInfo = new PageInfo<>(productCommentsVOList);

        PageVO<ProductCommentsVO> pageVO = new PageVO<>();
        pageVO.setCount((int) productCommentsVOPageInfo.getTotal());  // 总条数
        pageVO.setPageCount(productCommentsVOPageInfo.getPages());  //总页数
        pageVO.setList(productCommentsVOList);

        if (productCommentsVOList.size() == 0){
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("没有查到该商品的评论信息");
        }else{
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
            resultVO.setData(pageVO);
        }

        return resultVO;
    }
}
